package com.leji.song.mainPage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.dgl.sdk.view.LoadImage;
import com.leji.song.R;
import com.leji.song.mainPage.model.DS_Goods;

import java.util.List;

/**
 * Created by mac on 2014/6/14.
 */
public class GoodsAdapter extends BaseAdapter {
    protected ListItemView listItemView;
    protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_Goods> goodsList;

    public interface BuyGoodsCallback{
         public void onClick(DS_Goods goods);
    }

    private BuyGoodsCallback buyGoodsCallback;
    public GoodsAdapter(){};

    public GoodsAdapter(Context context, List<DS_Goods> goodsList,BuyGoodsCallback buyGoodsCallback){
        this.context = context;
        this.goodsList = goodsList;
        this.buyGoodsCallback = buyGoodsCallback;

        listContainer = LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    protected  class ListItemView{
        public ImageView goodsImg;
        public TextView name;
        public TextView price;
        public TextView number;
        public TextView decress;
        public TextView add;


    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.shop_menu_adapter, null);
            // 获取控件对象
            listItemView.goodsImg = (ImageView) convertView.findViewById(R.id.goodsImg);
            listItemView.name = (TextView) convertView.findViewById(R.id.name);
            listItemView.price = (TextView) convertView
                    .findViewById(R.id.price);
            listItemView.number = (TextView) convertView
                    .findViewById(R.id.number);
            listItemView.decress = (TextView) convertView
                    .findViewById(R.id.decress);
            listItemView.add = (TextView) convertView
                    .findViewById(R.id.add);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }


        LoadImage.display(listItemView.goodsImg, goodsList.get(position).getImgUrl());
        listItemView.name.setText(goodsList.get(position).getName());

        Float price = 0.0f;
        price = Float.valueOf(goodsList.get(position).getPrice());
        listItemView.price.setText("¥：" + Float.toString(price));

        listItemView.number.setText(Integer.toString(goodsList.get(position).getBuyNum()));

        listItemView.decress.setOnClickListener(new DecressClickListener(position));
        listItemView.add.setOnClickListener(new AddClickListener(position));


        return convertView;
    }

    class DecressClickListener implements View.OnClickListener {
        private int position;

        public DecressClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {

            if(goodsList.get(position).getBuyNum() ==0){
                return;
            }

            goodsList.get(position).setBuyNum(goodsList.get(position).getBuyNum()-1);

            buyGoodsCallback.onClick(goodsList.get(position));

            notifyDataSetChanged();

        }

    }

    class AddClickListener implements View.OnClickListener {
        private int position;

        public AddClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
            goodsList.get(position).setBuyNum(goodsList.get(position).getBuyNum()+1);

            buyGoodsCallback.onClick(goodsList.get(position));

            notifyDataSetChanged();

        }

    }
}
